#!perl

use strict;
use Text::CSV;

require "data.pl";
require "db.pl";

sub GenProjectRecord
{
	my (@fields) = @_;
	my $dev_id = $fields[1];
	my $custom_name = $fields[2];
	my $floor_type = $fields[3];
	my $custom_type = $fields[4];
	my $contract_type = $fields[5];
	my $dev_floor = $fields[6];
	my $action_date = $fields[7];
	if ($action_date =~ /(\d+)[-\/\.](\d+)[-\/\.](\d+)/) {
		$action_date = $1*10000 + $2*100 + $3;
	} else {
		$action_date = 20110101;
	}
	my $zoon = $fields[8];
	my $server = $fields[9];
	my $line = $fields[10];

	my @prop;
	push(@prop, undef);
	push(@prop, undef);
	push(@prop, undef);
	push(@prop, $action_date);
	push(@prop, 0);
	push(@prop, undef);
	push(@prop, $dev_id);
	push(@prop, $custom_name);
	push(@prop, $dev_floor);
	push(@prop, $contract_type);
	push(@prop, $custom_type);
	push(@prop, $floor_type);
	my $p = new_project(\@prop, 0);
	my @route = ($zoon, $server, $line);
	return ($p, \@route);
}

sub load_file_projects
{
	my ($file_name, $root) = @_;
	my $csv = Text::CSV->new({binary=>1,});
	open IN_FILE, "<$file_name" or die "input file open fail";
	my @lines = <IN_FILE>;

	foreach(@lines)
	{
		chomp;
		my $status = $csv->parse($_);
		my @columns = $csv->fields();
		my @project_route = GenProjectRecord(@columns);
		merge_project($root, $project_route[1], $project_route[0]);
	}
	close IN_FILE;
}

sub UpdateResourceRecord
{
	my ($r0, $r1) = @_;
	$r0->{name} = $r1->{name};
	$r0->{username} = $r1->{username};
	$r0->{password} = $r1->{password};
	$r0->{work_title} = $r1->{work_title};
	$r0->{work_level} = $r1->{work_level};
}

sub GenResourceRecord
{
	my (@fields) = @_;
	my $work_id = $fields[0];
	my $name = $fields[1];
	my $username = $fields[2];
	my $password = $fields[3];
	my $work_title = $fields[4];
	my $work_level = $fields[5];

	my $zoon = $fields[6];
	my $server = $fields[7];
	my $line = $fields[8];

	my $login_enable = $fields[9];

	my @prop;
	push(@prop, undef);
	push(@prop, 20);
	push(@prop, $work_id);
	push(@prop, $work_title);
	push(@prop, $work_level);
	push(@prop, undef);
	push(@prop, $name);
	push(@prop, $username);
	push(@prop, $password);
	push(@prop, $login_enable);
	my $p = new_resource(\@prop, 0);
	my @route = ($zoon, $server, $line);
	return ($p, \@route);
}

sub load_file_resources
{
	my ($file_name, $resource_root, $project_root) = @_;
	my $csv = Text::CSV->new({binary=>1,});
	open IN_FILE, "<$file_name" or die "input file open fail";
	my @lines = <IN_FILE>;

	foreach(@lines)
	{
		chomp;
		my $status = $csv->parse($_);
		my @columns = $csv->fields();
		my @resource_route = GenResourceRecord(@columns);
		my $r = $resource_route[0];
		if (exists($resource_root->{$r->{work_id}}))
		{
			my $r0 = $resource_root->{$r->{work_id}};
			UpdateResourceRecord($r0,$r);
			$r = $r0;
			update_resource($r);
		}
		else
		{
			insert_resource($r);
		}
		merge_resource($project_root, $resource_route[1], $r);
	}
	close IN_FILE;
}

1;
